<?php
/**
 * 不参加统计操作类
 *
 * @version 1.0
 * @date 2013-12-13
 */
class DefaultController extends FController {
	public function accessRules() {
		$config = array('index' => 1);
		return ZRole::auth("nojoinrule", $config);
	}
	public function __common() {
		parent::init();
		if (empty($this->cLoginUser)) {
			$this->redirect($this->createUrl('/site/login'));
		}
		$this->commonScript();
		Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
	}
	//列表显示
	public function actionIndex() {
		$this->__common();
		$this->title = '不参加统计规则设置';
		$s['code'] = Yii::app()->request->getParam('code');
		$s['name'] = Yii::app()->request->getParam('name');
		$s = Func::dis_input($s);
		$s = Func::fAddslashes($s);
		$where = ' AND "is_del" = 0 ';
		$order = ' ORDER BY "code" ';
		$limit = 8;
		# 拼接where #
		if(Func::check_search($s['code'])){
			$where.= ' AND "code" like \'%'.$s['code'].'%\'';
		}
		if(Func::check_search($s['name'])){
			$where.= ' AND "name" like \'%'.$s['name'].'%\'';
		}
		$sql = 'SELECT * FROM "{{nojoinrule}}" WHERE 1=1 '.$where.$order;
		$count = 'SELECT COUNT("id") FROM "{{nojoinrule}}" WHERE 1=1 '.$where;
		$data = ZCommon::getDaoPageList($sql, $count, $limit);
		$pageUrl = $this->createUrl('/nojoin_rule/default/index',$s);
		$this->render('index',array(
		'data'	=>$data['listDatas'],
		'pages' => $data['pages'],
		'serch'	=> $s,
		'pageUrl' => $pageUrl
		));
	}
	//更新
	public function actionUpdate(){
		$this->layout = "//layouts/main_window";
		$id = Yii::app()->request->getparam('id');
		$model = Nojoinrule::model();
		if(!empty($_POST['submit'])){
			
			$data = ZTask::dis_input(Yii::app()->request->getParam('info'));
			$data['status'] = (!empty($data['status'])) ? $data['status'] : 0;
			$data['code'] = strtolower($data['code']);
			
			
				$codecount = $model->find('"id" != '.$data['id'].' AND "code"=\''.$data['code'].'\'');
				if (!empty($codecount)){
					if($codecount->is_del == 1){
						$this->frameAjaxMessage(null,'相同代码已在回收站种,不可重复使用');	
					}else{
						$this->frameAjaxMessage(null,'代码已被占用!');
					}					
					exit;
				}
			
			$model = $model->findByPk($data['id']);
			$model->code=trim($data['code']);
			$model->name=trim($data['name']);
			$model->status=$data['status'];
			$model->remark=trim($data['remark']);
			$model->update_user = $this->cLoginUser['cuser']['id'];
			$model->update_time = time();
			if($model->save()){
				$this->frameAjaxMessage('/nojoin_rule/default/index');
				exit;
			}else{
				$this->frameAjaxMessage(null,"操作失败!");
				exit;
			}
		}else{
			$this->render('update',array('data'	=> $model->findByPk($id)->attributes));
		}
	}
	//添加
	public function actionAdd(){
		$this->layout = "//layouts/main_window";
		if(!empty($_POST['submit'])){
			$nojoinrule = new Nojoinrule();
			$data = ZTask::dis_input(Yii::app()->request->getParam('info'));
			$data['code'] = strtolower($data['code']);
			$data['status'] = (!empty($data['status'])) ? $data['status'] : 0;
            $data['is_del'] = 0;
				$codecount = $nojoinrule->find('"code"=\''.$data['code'].'\'');
				if (!empty($codecount)){
					if($codecount->is_del == 1){
						$this->frameAjaxMessage(null,'相同代码已在回收站种,不可重复使用');	
					}else{
						$this->frameAjaxMessage(null,'代码已被占用!');
					}					
					exit;
				}

			$data['add_time']= time();
			$data['create_user']= $this->cLoginUser['cuser']['id'];
			$data['create_time']= time();
			$nojoinrule->attributes=$data;
			if ($nojoinrule->save()) {
				$this->frameAjaxMessage('/nojoin_rule/default/index');
				exit;
			}else{
				$this->frameAjaxMessage(null,'数据添加失败');
				exit;
			}
		}else{
			$this->render('add');
		}
	}
	//删除
	public function actionDelete(){
		$id = (int)Yii::app()->request->getparam('id');
		if(!empty($id)){
			$model = Nojoinrule::model()->findByPk($id);
			if(empty($model)){
				$this->frameAjaxMessage(null,"您要删除的数据不存在!");exit;
			}
			$model->is_del=1;
			if ($model->save()){
				$models = NojoinruleDetail::model()->find('"join_id"='.$id);
				if (!empty($models)){
					$models->is_del=1;
					$models->save();
				}
				$this->frameAjaxMessage('/nojoin_rule/default/index');
				exit;
			}else {
				$this->frameAjaxMessage(null,"删除失败!");
				exit;
			}
		}else{
			$this->frameAjaxMessage(null,"删除失败,参数错误!");
			exit;
		}
	}
	
	/**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			
			$select = 'SELECT "join_id" FROM "{{nojoinrule_detail}}" WHERE "join_id" IN ('.$id.') AND "is_del" = 0';
   			$data = Yii::app()->db->createCommand($select)->queryRow();
   			if(!empty($data)){
   				echo 3;
   				exit;
   			}
   			
			$delete = 'UPDATE "{{nojoinrule}}" SET "is_del" = 1 WHERE "id" IN ('.$id.')';
			$delete1 = 'UPDATE "{{nojoinrule_detail}}" SET "is_del" = 1 WHERE "join_id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			$query = Yii::app()->db->createCommand($delete1)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}
}